<template>
  <div class="item-one" ref="itemOne">
    <div class="title">
      <span>银行到账查询</span>
      <div>
        <!-- <el-button type="primary">导出</el-button> -->
        <el-button icon="el-icon-refresh"></el-button>
      </div>
    </div>
    <div class="container">
      <div class="screeningBox">
        <div class="leftBox">
          <div class="leftitem">
              <el-date-picker
                v-model="ctime"
                :clearable='false'
                type="daterange"
                align="center"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :picker-options="pickerOptions">
              </el-date-picker>
          </div>
        </div>
        <div class="rightBox">
          <div class="rightitem" v-show="rolesId !== 5">
              <!-- <el-input class="merchants" v-model="conditions.shopno" placeholder="商户号/商户名称"></el-input> -->
              <el-form ref="conditions" :model="conditions" label-width="80px">
                <el-input class="width60" v-model.trim="conditions.merchantNo" placeholder="请输入商户号"></el-input>
                <el-input class="width60" v-model.trim="conditions.merchantName" placeholder="请输入商户名称"></el-input>
              </el-form>
              <!-- <el-autocomplete
                class="inline-input"
                v-model="conditions.shopno"
                @keyup.enter.native="soso"
                :fetch-suggestions="querySearch"
                placeholder="请输入商户名称"
                @select="handleSelect"
              ></el-autocomplete> -->
          </div>
          <div class="rightitem">
            <el-button icon="el-icon-search" type="primary" @click="soso" :disabled="isDisable">搜索</el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="reset">重置</el-button>
          </div>
        </div>
      </div>
     
      <div class="tableBox">
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :data="merchantarr"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          stripe
          :header-cell-style="tableHeaderColor"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column align="center" type="index" label="序号"></el-table-column>
          <el-table-column prop="settleTime" align="center" label="清算时间"></el-table-column>
          <el-table-column prop="price" align="center" label="清算金额(元)"></el-table-column>
          <el-table-column prop="statusName" align="center" label="清算状态"></el-table-column>
          <el-table-column prop="statusMsg" align="center" label="状态描述"></el-table-column>
          <el-table-column prop="channelName" align="center" label="清算通道"></el-table-column>
        </el-table>
        <div class="channltype" v-if="rolesId == 5 && inchannlof == true">
          <span style="color:red;">*</span>您当前使用的通道不支持查询！
        </div>
        <div class="channltype" v-if="rolesId == 1 || rolesId == 2 || rolesId == 3 || rolesId == 4">
          <span style="color:red;">*</span>只支持网商、富友、天阙、汇付和随行付通道的查询！
        </div>
        <!-- <div class="pagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            @size-change="sizechange"
            :page-sizes="[10,20,30,40,50]"
            :page-size="cur_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total1"
          ></el-pagination>
        </div> -->
      </div>
      <br>
      <br>
    </div>
  </div>
</template>

<script>
import { getservice, getBankAccount, getUserSelect, merchantdetails, channel } from "@/api/api";
export default {
  name: "item-one",
  data: function() {
    return {
      loading: false,
      isDisable: false,
      cur_page: 1,
      cur_size: 10,
      multipleSelection: [],
      restaurants:[],
      visible: false,
      merchantarr: [],
      total1: 0,
      rolesId:'',
      fid: '',
      fid1:'',
      pageCount: "",
      ctime: '',
      conditions: {
        merchantsstate: "",
        merchantsno: "",
        stardate: "",
        enddate: "",
        aisle: "",
        shopno: "",
        merchantNo:'',
        merchantName:''
      },
      usname:'',
      merchantsid:'',
      inchannlof: false,
      pickerOptions: {
        onPick: ({maxDate, minDate}) => {
        this.selectDate= minDate.getTime();
        if (maxDate) {
          this.selectDate= ''
        }
      }, disabledDate: (time) => {
        if (this.selectDate!== '') {
          const one = 30 * 24 * 3600 * 1000;
          const minTime = this.selectDate- one;
          const maxTime = this.selectDate+ one;
          return time.getTime() < minTime || time.getTime() > maxTime
        }
      },
        shortcuts: [
          {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', [date,date]);
            }
          },
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, 
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          },
        ]
      },
    };
  },
  created() {
    this.getPermissions()
    this.merchantsid = sessionStorage.getItem("merchantsid")
     const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
    const year=start.getFullYear();
    const month=start.getMonth()+1;
    const date=start.getDate();
    if(month < 10){
      const date1 = year+'-'+ 0 +month+ '-'+date +' 00:00:00'
      const date2 = year+'-'+ 0 +month+ '-'+date +' 23:59:59'
      this.ctime =  [date1,date2]
    }else{
      const date1 = year+'-'+month+ '-'+date +' 00:00:00'
      const date2 = year+'-'+month+ '-'+date +' 23:59:59'
      this.ctime =  [date1,date2]
    }
    this.channel()
  },
  methods: {
    // 修改table tr行的背景色
    // tableRowStyle({ row, rowIndex }) {
    //   return 'background-color: pink'
    // },
    //修改thead 背景色
     tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #F0F0F0;font-weight: 500;font-size:14px;color:#000;'
      }
    },
    // 刷新
    refresh() {
      this.$router.replace({
        path: "/refresh",
        query: {
          t: Date.now()
        }
      });
    },
    // 分页导航
    handleCurrentChange(val) {
      this.cur_page = val;
      this.merchantlist();
      this.$nextTick(() => {
        document.querySelectorAll(".content")[0].scrollTop = "0";
      });
    },
    // 分页数量
    sizechange(val) {
      this.cur_size = val;
      this.merchantlist();

      this.$nextTick(() => {
        document.querySelectorAll(".content")[0].scrollTop = "0";
      });
    },
    //多选框
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //  重置
    reset() {
      // this.ctime = "";
      this.conditions.merchantNo = "";
      this.conditions.merchantName = ""
    },
    //获取权限
    getPermissions(){
      var aaa = sessionStorage.getItem('userInfo')
      var bbb = JSON.parse(aaa)
      this.rolesId = bbb.rolesId
      this.fid1 = bbb.fid
      this.usname = bbb.username
    },
    // 搜索
    soso() {
      // if (this.conditions.servicelevel == 1) {
      //   this.merchantlist("agent1Id");
      // } else if (this.conditions.servicelevel == 2) {
      //   this.merchantlist("agen2Id");
      // } else {
      //   this.merchantlist("agent1Id");
      // }
      this.merchantlist();
      this.fid = ''
    },
    // 通道列表
    channel() {
      var channelType = []
      channel({
        merchantid: this.merchantsid
      }).then(res => {
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else {
          if(res.data.rows.length > 0){
            for (var i = 0; i < res.data.rows.length; i++){
              if(res.data.rows[i].channelType !== 1 || res.data.rows[i].channelType !== 2 || res.data.rows[i].channelType !== 4){
                this.inchannlof = true
              }
            }
          }
        }
      });
    },
    //商户模糊查询
    querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
        getUserSelect({ //商户模糊查询
        roleId: 5,
        keywords: this.conditions.shopno
      }).then(res => {
        console.log(res)
        if(res.code == 200){
          this.restaurants = []
          for(var i=0;i<res.data.length;i++){
            this.restaurants.push({'value':res.data[i].fname,'id':res.data[i].fid})
          }
        }
        console.log(this.restaurants)
      })
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !==-1);
      };
    },
    handleSelect(item) {
      console.log(item);
      this.fid = item.id
      // this.merchantlist(item.id) 
    },
    //当值发生改变时
    // handleSelect1(){
    //   console.log(1111111)
    //   console.log(this.fid)
    // },
    // 商户列表
    merchantlist() {
      // if(this.conditions.shopno !== ''){
      //   for(var i = 0; i < this.restaurants.length; i++){
      //     if(this.conditions.shopno == this.restaurants[i].value){
      //       this.fid  = this.restaurants[i].id
      //     }
      //   }
      // }
      if(this.rolesId == 5){
        this.conditions.merchantNo = this.usname
      }
      this.loading = true
      this.isDisable = true
        getBankAccount({
          // page: this.cur_page,
          // size: this.cur_size,
          // startDate: this.ctime,
          startDate: this.ctime[0],
          endDate: this.ctime[1],
          // merchantId: this.fid,
          merchantNo: this.conditions.merchantNo,
          merchantName: this.conditions.merchantName
        }).then(res => {
          if (res.code == 405) {
            sessionStorage. removeItem("token");
            this.$router.push("/login");
          } else {
            if(res.code == 405){
              sessionStorage. removeItem('token')
              this.$router.push('/login');
            }else if(res.code == 200){
              this.isDisable = false
              this.loading = false
              if(res.data!== []){
                this.merchantarr = res.data;
              }
            }else if(res.code == 500){
              this.isDisable = false
              this.loading = false
              this.$message.error(res.message);
              this.merchantarr = []
            }
          }
        });
    }
  }
};
</script>
<style scoped lang='less'>
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 40%;
  padding: 0 15px;
  box-sizing: border-box;
}
.leftBox > .leftitem {
  margin-bottom: 15px;
}
.rightBox > .rightitem {
  margin-bottom: 15px;
}
.tableBox {
  margin-top: 20px;
}
.red {
  color: #f9607d;
}
.blue {
  color: #01aaef;
}
// .el-input--small{
//   width: 500px;
// }
.inline-input{
  width: 100%;
}
.width60{
  width: 50%;
}
.channltype{
  margin: 10px;
  text-align: center;
  font-size: 14px;
  color: #909399
}
</style>